package br.com.logistica.mbean ;

import java.io.Serializable ;
import java.util.List ;

import javax.faces.event.ActionEvent ;

import org.apache.log4j.Logger ;

import br.com.logistica.delegate.BusinessDelegate ;
import br.com.logistica.util.dto.ServiceDTO ;
import br.com.logistica.vo.Transportadora ;


public class TransportadoraManagedBean implements Serializable
{
    private static Logger        logger          = Logger.getLogger( TransportadoraManagedBean.class ) ;
    
    private List<Transportadora> transportadoras = null ;
    private ServiceDTO           requestDTO      = new ServiceDTO    ( ) ;
    private ServiceDTO           responseDTO     = new ServiceDTO    ( ) ;
    private Transportadora       transportadora  = new Transportadora( ) ;

    
    public TransportadoraManagedBean( )
    {
        transportadoras = atualizarListaTransportadora( ) ;
    }
    
    private List<Transportadora> atualizarListaTransportadora( )
    {
        List<Transportadora> list = null ;
        
        try
        {
            responseDTO     = BusinessDelegate.getInstance( ).pesquisarTodasTransportadoras( ) ;
            list            = (List<Transportadora>)responseDTO.get( "transportadora" ) ;
            transportadoras = list ;
        }
        catch( Exception e )
        {
            logger.error( e ) ;
        }
        
        return( list ) ;
    }

    private void limparServiceDTO( )
    {
        transportadora = new Transportadora( ) ;
        requestDTO     = new ServiceDTO    ( ) ;
        responseDTO    = new ServiceDTO    ( ) ;
    }

    public void inserir( ActionEvent actionEvent )
    {
        requestDTO.set( "transportadora", transportadora ) ;
        
        try
        {
            responseDTO = BusinessDelegate.getInstance( ).inserirTransportadora( requestDTO ) ;
            
            if ( responseDTO.get( "operacao" ).equals( "persistencia" ) )
            { //cadastro
            }
            else
            { //alteracao
            }
            
            limparServiceDTO            ( ) ;
            atualizarListaTransportadora( ) ;
        }
        catch( Exception e )
        {
            logger.error( e ) ;
        }
    }

    public void deletarTransportadora( ActionEvent actionEvent )
    {
        requestDTO.set( "transportadora", transportadora ) ;
        
        try
        {
            responseDTO = BusinessDelegate.getInstance( ).deletarTrasnportadora( requestDTO ) ;
            
            limparServiceDTO            ( ) ;
            atualizarListaTransportadora( ) ;
        }
        catch( Exception e )
        {
            logger.error( e ) ;
        }
    }

    public Transportadora getTransportadora( )
    {
        return( transportadora ) ;
    }

    public void setTransportadora( Transportadora transportadora )
    {
        this.transportadora = transportadora ;
    }

    public List<Transportadora> getTransportadoras( )
    {
        return( transportadoras ) ;
    }

    public void setTransportadoras( List<Transportadora> transportadoras )
    {
        this.transportadoras = transportadoras ;
    }
}